#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > geoopt_exci.info <<'%EOF%'
   geoopt_exci
   -----------
   Molecule:         H2O
   Wave Function:    MCSCF / aug-cc-pVDZ
   Test Purpose:     Geometry optimization of the first-excited state
                     of A1 symmetry in the molecule using the .OPTIMIZE
                     module (2nd order). Note that this test will crash,
                     due to the need for breaking the symmetry, which we
                     do not allow for an MCSCF wave function
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > geoopt_exci.mol <<'%EOF%'
BASIS
aug-cc-pVDZ
Geometry optimization of excited state. Breaking of symmetry leads to
error message
Atomtypes=2
Charge=1.0 Atoms=2
H     1.45235 0.899623 0.0
H    -1.45235 0.899623 0.0
Charge=8.0 Atoms=1
O     0.0 -0.2249058930 0.0
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > geoopt_exci.dal <<'%EOF%'
**DALTON INPUT
.OPTIMIZE
*OPTIMIZE
.2NDORDER
**WAVE FUNCTION
.HF
.MP2
.MCSCF
.NSYM
 4
*SCF INPUT
.DOUBLY OCCUPIED
 3 1 1 0
*CONFIGURATION INPUT
.SYMMETRY
 1
.SPIN MUL
 1
.INACTIVE
 1 0 0 0
.ELECTRONS
 8
.CAS SPACE
 4 2 2 0
*OPTIMIZATION
.DETERM
.SIMULTANEOUS ROOTS
 2 2
.STATE
 2
.OPTIMAL ORBITAL TRIAL VECTORS
**PROPERTIES
.VIBANA
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >geoopt_exci.check
cat >>geoopt_exci.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

# Geometry optimization
CRIT1=`$GREP "2nd Order Geometry Optimization" $log | wc -l`
TEST[1]=`expr	$CRIT1`
CTRL[1]=1
ERROR[1]="GEOMETRY OPTIMIZATION SETUP NOT CORRECT"

# Symmetry
CRIT1=`$GREP "Full point group is\: * C\(2v\)" $log | wc -l`
CRIT2=`$GREP "Represented as\: * C2v" $log | wc -l`
CRIT3=`$GREP "Reflection in the yz\-plane" $log | wc -l`
CRIT4=`$GREP "Reflection in the xz\-plane" $log | wc -l`
CRIT5=`$GREP "Number of coordinates in each symmetry\: * 3 * 2 * 3 * 1" $log | wc -l`
CRIT6=`$GREP "Number of orbitals in each symmetry\: * 18 * 7 * 12 * 4" $log | wc -l`
TEST[2]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6`
CTRL[2]=6
ERROR[2]="SYMMETRY NOT CORRECT"

# Initial energy
CRIT1=`$GREP "Hartree\-Fock total energy *\: * \-76\.040180372" $log | wc -l`
CRIT2=`$GREP "\= MP2 second order energy *\: * \-76\.2632882.." $log | wc -l`
CRIT3=`$GREP "Final MCSCF energy\: * \-75\.7939317218" $log | wc -l`
TEST[3]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[3]=3
ERROR[3]="INITIAL ENERGY NOT CORRECT"

# Initial gradient
CRIT1=`$GREP "H * y * (\-0|\-)\.144(499|500)...." $log | wc -l`
CRIT2=`$GREP "H * z * (0| )\.0274(2|3)....." $log | wc -l`
CRIT3=`$GREP "O * z * (\-0|\-)\.0274(2|3)....." $log | wc -l`
TEST[4]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[4]=3
ERROR[4]="INITIAL GRADIENT NOT CORRECT"

# Initial Hessian
CRIT1=`$GREP "H * y * (0| )\.76325." $log | wc -l`
CRIT2=`$GREP "H * z * (0| )\.39120. * (0| )\.3544(59|60)" $log | wc -l`
CRIT3=`$GREP "O * z * (\-0|\-)\.39120. * (\-0|\-)\.3544(59|60) * (0| )\.3544(59|60)" $log | wc -l`
CRIT4=`$GREP "H * x * (0| )\.02439." $log | wc -l`
CRIT5=`$GREP "O * x * (\-0|\-)\.02439. * (0| )\.02439." $log | wc -l`
CRIT6=`$GREP "H * y * (0| )\.54739." $log | wc -l`
CRIT7=`$GREP "H * z * (0| )\.40495. * (0| )\.21405." $log | wc -l`
CRIT8=`$GREP "O * y * (\-0|\-)\.54739. * (\-0|\-)\.40495. * (0| )\.54739." $log | wc -l`
CRIT9=`$GREP "H * x * (\-0|\-)\.09949." $log | wc -l`
TEST[5]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9`
TEST[5]=9 # too sensitive
CTRL[5]=9
ERROR[5]="INITIAL HESSIAN NOT CORRECT"

# First step
CRIT1=`$GREP "H *_1 * (0| )\.0000000000 * 1\.60408(6|7)..[0-9][0-9] * (0| )\.94631[2-4]..[0-9][0-9]" $log | wc -l`
CRIT2=`$GREP "H *_2 * (0| )\.0000000000 * \-1\.60408(6|7)..[0-9][0-9] * (0| )\.94631[2-4]..[0-9][0-9]" $log | wc -l`
CRIT3=`$GREP "O * (0| )\.0000000000 * (0| )\.0000000000 * (\-0|\-)\.02112[2-5]..[0-9][0-9]" $log | wc -l`
TEST[6]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[6]=3
ERROR[6]="FIRST STEP NOT CORRECT"

# Second iteration
CRIT1=`$GREP "Energy at this geometry is * \: * \-75\.816478" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * (0| )\.07619" $log | wc -l`
TEST[7]=`expr	$CRIT1 \+ $CRIT2`
CTRL[7]=2
ERROR[7]="SECOND ITERATION NOT CORRECT"

# Third iteration
CRIT1=`$GREP "Energy at this geometry is * \: * \-75\.8359(79|80)" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * (0| )\.05621" $log | wc -l`
TEST[8]=`expr	$CRIT1 \+ $CRIT2`
CTRL[8]=2
ERROR[8]="THIRD ITERATION NOT CORRECT"

# Last geometry
CRIT1=`$GREP -l "H *_1 * (0| )\.0000000000 * 2\.4781[0-9]* * (0| )\.6238[0-9]*" $log | wc -l`
CRIT2=`$GREP -l "H *_2 * (0| )\.0000000000 * \-2\.4781[0-9]* * (0| )\.6238[0-9]*" $log | wc -l`
CRIT3=`$GREP -l "O * (0| )\.0000000000 * (0| )\.0000000000 * (0| )\.6238[0-9]*" $log | wc -l`
TEST[9]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[9]=3
ERROR[9]="LAST GEOMETRY NOT CORRECT"

# Last iteration
CRIT1=`$GREP -l "Energy at this geometry is * \: * \-75\.889522" $log | wc -l`
CRIT2=`$GREP -l "Norm of gradient * \: * (0| )\.0000" $log | wc -l`
CRIT3=`$GREP -l "Norm of step * \: * (0| )\.0000" $log | wc -l`
TEST[10]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[10]=3
ERROR[10]="LAST ITERATION NOT CORRECT"

# Breaking of symmetry
#CRIT1=`$GREP "Due to non\-zero index of total Hessian\, symmetry \# * 2" $log | wc -l`
#CRIT2=`$GREP "has to be broken to minimize energy\!" $log | wc -l`
#TEST[11]=`expr	$CRIT1 \+ $CRIT2`
#CTRL[11]=2
#ERROR[11]="BREAKING OF SYMMETRY NOT CORRECT"

# Error message
#CRIT1=`$GREP "HF occupation must be specified when SCF followed by CI or MCSCF" $log | wc -l`
#TEST[12]=`expr	$CRIT1`
#CTRL[12]=1
#ERROR[12]="PROGRAM HALT NOT CORRECT"

PASSED=1
for i in 1 2 3 4 5 6 7 8 9 10
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} : ${TEST[i]} .ne. ${CTRL[i]} ; "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
